package zhoujunhao.rbac.service.array;

import zhoujunhao.rbac.domian.dto.RoleDto;
import zhoujunhao.rbac.domian.entity.Role;
import zhoujunhao.rbac.domian.vo.Result;
import zhoujunhao.rbac.domian.vo.RoleVo;
import zhoujunhao.rbac.service.IRoleService;
import zhoujunhao.rbac.service.IUserService;

/**
 * 业务层-部门
 *
 * @author 周均壕
 * @since 2025/6/16 03:51
 */
public class RoleService extends BaseService<Role, RoleDto, RoleVo> implements IRoleService {


    private static IRoleService singleton;
    private IUserService userService;


    private RoleService() {
        date = new Role[DATA_SIZE];
        temp = new RoleVo[DATA_SIZE];
    }

    public static IRoleService getInstance() {
        if (singleton == null) {
            singleton = new RoleService();
        }
        return singleton;
    }

    @Override
    RoleVo convertToVo(Role role) {
        return new RoleVo(role.getId(), role.name, role.dataScope);
    }

    @Override
    Role createEntity(RoleDto dto, int id) {
        return new Role(id, dto.name, dto.dataScope);
    }

    @Override
    void updateEntity(RoleDto dto, Role role) {
        role.name = dto.name;
        role.dataScope = dto.dataScope;
    }

    @Override
    public Result delete(int id) {
        if (!existsByid(id)) {
            return Result.failure("删除失败，不存在此数据");
        }
        if (userService == null) {
            userService = UserService.getInstance();
        }
        if (userService.roleInUse((id))) {
            return Result.failure("删除失败，还有角色属于该部门，不能删除");
        }
        date[id] = null;
        return Result.success("删除成功", null);
    }
}
